<template>
  <div>
    <!--面包屑导航区-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>
    <!--卡片视图区域-->
    <el-card>
      <!--提示区域-->
      <el-alert title="添加商品信息"
                type="info"
                center
                show-icon :closable="false"></el-alert>
      <!--步骤条区域-->
      <el-steps :space="200" :active="activeIndex - 0" finish-status="success" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="商品参数"></el-step>
        <el-step title="商品属性"></el-step>
        <el-step title="商品图片"></el-step>
        <el-step title="商品内容"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>
      <!--tabs区域: 表单区域-->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px" label-position="top">
      <el-tabs v-model="activeIndex" :tab-position="'left'" :before-leave="beforeTabLeave" @tab-click="tabClicked">
        <el-tab-pane label="基本信息" name="0">
          <el-form-item label="商品名称" prop="name">
            <el-input v-model="addForm.name"></el-input>
          </el-form-item>
          <el-form-item label="商品分类" prop="selectedCateKeys">
            <el-cascader
              v-model="addForm.selectedCateKeys" :props="cateProps"
              :options="catelist"
              @change="handleChange"></el-cascader>
          </el-form-item>
          <el-form-item label="商品描述" prop="description">
            <el-input v-model="addForm.description"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="price">
            <el-input v-model="addForm.price" type="number"></el-input>
          </el-form-item>
          <el-form-item label="商品数量" prop="stock">
            <el-input v-model="addForm.stock" type="number"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="商品参数" name="1">
          <!--渲染表单的item项-->
          <el-form-item :label="item.name" v-for="item in manyTableData" v-bind:key="item.id">
            <!--复选框组-->
            <el-checkbox-group v-model="item.valueList">
              <el-checkbox border v-for="(vals) in item.valueList"  v-bind:key="vals.id" :label="vals">{{vals.attributeVal}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="商品属性" name="2">
          <el-form-item :label="item.name" v-for="item in onlyTableData" v-bind:key="item.id">
            <el-input  v-model="item.inputValue"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="商品图片" name="3">
          <el-form-item label="大图地址" prop="bigImg"><el-button @click="handlePreview(addForm.bigImg)" type="success">预览</el-button>
            <el-input v-model="addForm.bigImg"></el-input>
          </el-form-item>
          <el-form-item label="小图地址" prop="smallImg"><el-button @click="handlePreview(addForm.smallImg)" type="success">预览</el-button>
            <el-input v-model="addForm.smallImg"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="商品内容" name="4">
          <!--富文本编辑器组件-->
          <quill-editor v-model="addForm.introduce"></quill-editor>
          <!--添加商品按钮-->
          <el-button type="primary" class="btnAdd" @click="add">添加商品</el-button>
        </el-tab-pane>
      </el-tabs>
      </el-form>
    </el-card>
    <!--图片预览区域-->
    <el-dialog
      title="图片预览"
      :visible.sync="previewVisible"
      width="50%">
      <img :src="previewPath" class="previewImg"/>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: '0',
      // 添加商品的表单数据对象
      addForm: {
        name: '',
        price: 0,
        stock: 1000,
        description: '',
        // 级联选择框选中的key，商品所属的分类id
        selectedCateKeys: [],
        cateId: 0,
        smallImg: '',
        bigImg: '',
        // 详情描述
        introduce: '',
        manies: [],
        onlys: []
      },
      addFormRules: {
        name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
        price: [{ required: true, message: '请输入商品价格', trigger: 'blur' }],
        stock: [{ required: true, message: '请输入商品库存量', trigger: 'blur' }],
        selectedCateKeys: [{ required: true, message: '请选择商品分类', trigger: 'blur' }]
      },
      // 商品分类列表
      catelist: [],
      cateProps: {
        value: 'id',
        label: 'name',
        children: 'children',
        expandTrigger: 'hover',
        checkStrictly: 'false'
      },
      // 动态参数列表数据
      manyTableData: [],
      // 静态属性列表数据
      onlyTableData: [],
      previewPath: '',
      previewVisible: false
    }
  },
  created() {
    this.getCatesList()
  },
  methods: {
    // 获取所有商品分类数据
    async getCatesList() {
      const result = await this.$http.get('/category/getList', { params: { type: 3 } })
      if (result.data.code !== 200) {
        return this.$message.error('获取分类列表失败')
      }
      this.catelist = result.data.data
    },
    // 选择器变化会出发
    handleChange() {
      if (this.addForm.selectedCateKeys.length !== 3) {
        this.addForm.selectedCateKeys = []
        return false
      }
    },
    beforeTabLeave(activeName, oldActiveName) {
      if (oldActiveName === '0' && this.addForm.selectedCateKeys.length !== 3) {
        this.$message.error('请选择商品分类')
        return false
      }
    },
    // 切换tabs触发
    async tabClicked() {
      if (this.activeIndex === '1') {
        const result = await this.$http.get(`/attribute/getList/${this.cateId}`, { params: { sel: 'many' } })
        if (result.data.code !== 200) {
          return this.$message.error('获取动态参数失败！')
        }
        this.manyTableData = result.data.data
      } else if (this.activeIndex === '2') {
        const result = await this.$http.get(`/attribute/getList/${this.cateId}`, { params: { sel: 'only' } })
        if (result.data.code !== 200) {
          return this.$message.error('获取静态属性失败！')
        }
        this.onlyTableData = result.data.data
      }
    },
    // 预览按钮
    handlePreview(path) {
      this.previewPath = path
      this.previewVisible = true
    },
    // 添加商品
    add() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return this.$message.error('请填写必要的表单项！')
        // 处理商品分类id
        this.addForm.cateId = this.cateId
        // 处理动态参数
        this.addForm.manies = this.manyTableData
        // 处理静态属性
        this.addForm.onlys = this.onlyTableData
        const result = await this.$http.post('/goods/save', this.addForm)
        if (result.data.code !== 200) {
          this.$message.error('添加商品失败!')
        }
        this.$message.success('添加商品成功!')
        this.$router.push('/goods')
      })
    }
  },
  computed: {
    // 当前选中的三级分类的Id
    cateId () {
      if (this.addForm.selectedCateKeys.length === 3) {
        return this.addForm.selectedCateKeys[2]
      }
      return null
    }
  }
}
</script>

<style lang="less" scoped>
.el-checkbox{
  margin: 0 5px 0 0 !important;
}
.previewImg{
  width: 100%;
}
/deep/ .ql-editor{
  min-height:300px;
}
.btnAdd{
  margin-top: 15px;
}
</style>
